package cn.edu.scau.cmi.linhongxin.orm.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import cn.edu.scau.cmi.linhongxin.orm.domain.Student;
import cn.edu.scau.cmi.linhongxin.orm.domain.Teacher;

public class TeacherDAO extends BaseDAO{
	public void add(Teacher s) {
		Session session=getSession();
		Transaction tx=null;
		try {
			tx=session.beginTransaction();
			session.save(s);
			tx.commit();
		}catch (RuntimeException e) {
			// TODO: handle exception
			tx.rollback();
			throw e;
		}finally {
			session.close();
		}
	}
	public void update(Teacher s) {
		Session session=getSession();
		Transaction tx=null;
		try {
			tx=session.beginTransaction();
			session.update(s);
			tx.commit();
		}catch (RuntimeException e) {
			// TODO: handle exception
			tx.rollback();
			throw e;
		}finally {
			session.close();
		}
	}
	public void delete(int id) {
		Session session=getSession();
		Transaction tx=null;
		try {
			tx=session.beginTransaction();
			Object sObject=session.get(Teacher.class, id);
			session.delete(sObject);
			tx.commit();
		}catch (RuntimeException e) {
			// TODO: handle exception
			tx.rollback();
			throw e;
		}finally {
			session.close();
		}
	}
	public List<Teacher> findAll() {
		Session session=getSession();
		List<Teacher> list=null;
		try {
			String hql="from Teacher";
			@SuppressWarnings("unchecked")
			Query<Teacher> query=session.createQuery(hql);
			list=query.list();
		}catch (RuntimeException e) {
			// TODO: handle exception
			throw e;
		}finally {
			session.close();
		}
		return list;
	}
}
